package com.practice.car.cardataapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.CarSaleTrend;
import com.practice.car.cardataapp.entity.frontEnd.Form;
import com.practice.car.cardataapp.entity.frontEnd.Legend;
import com.practice.car.cardataapp.mapper.CarSaleTrendDao;
import com.practice.car.cardataapp.service.CarSaleTrendService;
import com.practice.car.cardataapp.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/6
 */
@Service
public class CarSaleTrendServiceImpl extends ServiceImpl<CarSaleTrendDao,CarSaleTrend> implements CarSaleTrendService {

	@Override
	public List<Form> getSeries(String year) {
		List<CarSaleTrend> carSaleTrends = this.list();
		carSaleTrends.sort((a,b)->{
			String[] aStrings = a.getMonth().split("-");
			String[] bStrings = b.getMonth().split("-");
			Integer aYear = Integer.valueOf(aStrings[0]);
			Integer bYear = Integer.valueOf(bStrings[0]);
			if (!Objects.equals(aYear, bYear)) {
				return aYear - bYear;
			} else {
				Integer aMonth = Integer.valueOf(aStrings[1]);
				Integer bMonth = Integer.valueOf(bStrings[1]);
				return aMonth - bMonth;
			}
		});
		ArrayList<CarSaleTrend> thisYearList = new ArrayList<>();
		carSaleTrends.forEach(c->{
			if (c.getMonth().contains(year)) {
				thisYearList.add(c);
			}
		});
		return getSeries(thisYearList);
	}

	private List<Form> getSeries(ArrayList<CarSaleTrend> thisYearList) {
		SeriesUtil<CarSaleTrend> util = new SeriesUtil<>();
		util.setLegendSolver(e -> e.getType());
		util.setFormSolver(e -> e.getSale());
		return util.getSeries(thisYearList);
	}

}
